package com.gamebox.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBTool {
	ResultSet rs = null;
	Statement stmt = null;
	
	public ResultSet excuteSelect(String sql) {
		Connection conn = DBDriver.getConnection();
		try {
			stmt = conn.createStatement();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery(sql);
		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
			rs = null;
		}
		
		return rs;
	}
	public boolean excuteUpdate(String sql) {
		Connection conn = DBDriver.getConnection();
		try {
			stmt = conn.createStatement();
			stmt.executeUpdate(sql); 
		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
			return false;
		}
		return true;
	}
	public boolean excuteDelete(String sql) {
		Connection conn = DBDriver.getConnection();
		try {
			stmt = conn.createStatement();
			stmt.execute(sql); 
		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
			return false;
		}
		return true;
	}
	public void closeQuery() {
		try {
			if(rs != null) {
				rs.close();
			}
			
		} catch (SQLException sqlEx) {
		} // ignore
		rs = null;
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException sqlEx) {
			} // ignore
			stmt = null;
		}
	}
}
